/*
 * Activity to call the service
 * 
 * Remember:
 * When a service is unbound from all clients, the Android system destroys it 
 * (unless it was also started with onStartCommand()).
 * 
 * SVN TAG:
 * "@(#) $Id: xxx.y 143715 2010-11-17 15:16:19Z wzbhsz $ $HeadURL: https://rzb-svn-dev.at.rzb.rbg.cc/systems/RZB_CAD/DB/trunk/cad/src/cad_calc.c $"
 */

package at.ticker.ebelticker;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import at.ticker.ebelticker.EBEL_TickerService.LocalBinder;


public class EBEL_TickerActivity extends Activity implements OnClickListener {
    EBEL_TickerService mService;
    boolean mBound = false;

    private static final String TAG = "ServicesDemo";
    Button mButtonStart, mButtonStop;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mButtonStart = (Button) findViewById(R.id.buttonStart);
        mButtonStop = (Button) findViewById(R.id.buttonStop);

        mButtonStart.setOnClickListener(this);
        mButtonStop.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, EBEL_TickerService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        
        // Beispiele, was man mit einem intent noch machen kann:
        //intent.putExtra("key","value");
        //intent.setAction("custom_action");    // ... um einen intent-filter anzusprechen
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Unbind from the service
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }

    // TODO notes about binding to a service:
    // You should always trap DeadObjectException exceptions, which are thrown when the connection has broken. This is the only exception thrown by remote methods.
    
    public void onClick(View src) {
    	switch (src.getId()) {
        case R.id.buttonStart:
          Log.d(TAG, "onClick: starting service");
          if (mBound) {
              // Call a method from the LocalService.
              // However, if this call were something that might hang, then this request should
              // occur in a separate thread to avoid slowing down the activity performance.
        	  
        	  //int num = mService.getRandomNumber();
        	  //Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
              //mService.sendNotification();
        	  //String actionString = mService.getETAction();
        	  ETAction actionObject = mService.getETActionObject();
        	  Toast.makeText(this, "ActionObject: " + actionObject.getAction(), Toast.LENGTH_SHORT).show();
          }
          //startService(intent);
          break;
        case R.id.buttonStop:
          Log.d(TAG, "onClick: stopping service");
          mService.onDestroy();
          //stopService(intent);
          break;
        }
      }

    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

    	// Called when the connection with the service is established
        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // Because we have bound to an explicit
            // service that is running in our own process, we can
            // cast its IBinder to a concrete class and directly access it.
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }

        // Called when the connection with the service disconnects unexpectedly
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
        	Log.e(TAG, "onServiceDisconnected");
        	mBound = false;
        }
    };

    
}